import Sortable from "sortablejs";

/**
 * @description 拖拽列
 * @param className 类名
 */
export const columnDrop = (
  className: string = ".el-table__header-wrapper tr",
  columns: any[]
) => {
  const tHeader = document.querySelector(className) as HTMLElement;
  if (!tHeader) return;
  Sortable.create(tHeader, {
    animation: 180,
    delay: 0,
    onEnd(e: any) {
      const { newIndex, oldIndex } = e;
      const currRow = columns.splice(oldIndex, 1)[0];
      columns.splice(newIndex, 0, currRow);
    },
  });
};

/**
 * @description 拖拽行
 * @param className 类名
 */
export const rowDrop = (
  className: string = ".el-table__body-wrapper tbody",
  callback: (e: any) => void
) => {
  const tbody = document.querySelector(className) as HTMLElement;
  if (!tbody) return;
  Sortable.create(tbody, {
    animation: 180,
    delay: 0,
    onEnd(e) {
      // const { newIndex, oldIndex } = e;
      // const currRow = data.splice(oldIndex, 1)[0];
      // data.splice(newIndex, 0, currRow);
      callback(e)
    },
  });
};
